package com.chaoxuan.uebt.activity;


import android.app.Dialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

import com.chaoxuan.uebt.R;
import com.chaoxuan.uebt.model.Uebt;
import com.chaoxuan.uebt.model.User;
import com.chaoxuan.uebt.utils.BaseSubscriber;
import com.chaoxuan.uebt.utils.CommomDialog2;
import com.chaoxuan.uebt.utils.Const;
import com.chaoxuan.uebt.utils.RetrofitUtils;
import com.chaoxuan.uebt.utils.CacheUtils;

public class WalletUebtActivity extends BaseActivity implements View.OnClickListener,TextWatcher {
    TextView tvBalance,tvFee;
    EditText editWalletUrl,editMin;
    TextView tvUebt;
    Button button;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_wallet_uebt);

        initTitle();
        tvUebt=(TextView)findViewById(R.id.tv_uebt);
        tvBalance=(TextView)findViewById(R.id.tv_balance);
        editWalletUrl=(EditText)findViewById(R.id.edit_wallet_url);
        editMin=(EditText)findViewById(R.id.edit_min);
        tvFee=(TextView)findViewById(R.id.tv_fee);
        findViewById(R.id.btn_address).setOnClickListener(this);
        findViewById(R.id.btn_all).setOnClickListener(this);
        button=(Button) findViewById(R.id.btn_confirm);
        button.setOnClickListener(this);
        init();
        editWalletUrl.addTextChangedListener(this);
        editMin.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {

            }

            @Override
            public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {

            }

            @Override
            public void afterTextChanged(Editable editable) {
                Double count=0.0;
                Double cost=0.0;
                if(!TextUtils.isEmpty(editable.toString())) {
                    count = Double.parseDouble(editable.toString());
                    cost=count*fee;
                    if(count>=100)
                        button.setSelected(false);
                    else{
                        button.setSelected(true);
                    }
                }else{
                    button.setSelected(true);
                }
                tvUebt.setText("到账数量："+String.format("%.2f", count-cost)+" UEBT");
                tvFee.setText("手续费："+cost+"（费率5%）");
            }
        });
        button.setSelected(true);
        load();
    }

    @Override
    public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {

    }

    @Override
    public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {

    }

    @Override
    public void afterTextChanged(Editable editable) {
        if(TextUtils.isEmpty(editWalletUrl.getText().toString())||TextUtils.isEmpty(editMin.getText().toString())){
            button.setSelected(true);
        }else{
            button.setSelected(false);
        }
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()){
            case R.id.btn_address:
                startActivity(new Intent(this,WalletModifyActivity.class));
                break;
            case R.id.btn_all:
                editMin.setText(tvBalance.getText());
                break;
            case R.id.btn_confirm:
                if(TextUtils.isEmpty(editMin.getText().toString()))return;
                Double min= Double.parseDouble(editMin.getText().toString());
                if(min<100){
                    new CommomDialog2(this, R.style.dialog, "最小兑换额度为100").setTitle("提示").show();
                    return;
                }
                Double balance= Double.parseDouble(tvBalance.getText().toString());
                if(min>balance){
                    new CommomDialog2(this, R.style.dialog, "可兑换积分不足").setTitle("提示").show();
                }else {
                    doApply();
                }
                break;
        }
    }
    Double fee=0.0;
    private void init(){
        User user= CacheUtils.getInstance(this).getUser();
        if(user==null)return;
        subscribe(RetrofitUtils.getAPIService().apply(user.getToken(),Const.DEVICE_TYPE), new BaseSubscriber<Uebt>() {
            @Override
            public void onSuccess(Uebt data) {
                tvBalance.setText(data.getBalance());
//                tvFee.setText("费率:"+data.getFee());
                fee=data.getFee();
            }
        });
    }
    private void doApply(){
        User user= CacheUtils.getInstance(this).getUser();
        if(user==null)return;
        String min=editMin.getText().toString();
        if(TextUtils.isEmpty(editWalletUrl.getText().toString())||TextUtils.isEmpty(min)){
            return;
        }
        subscribe(RetrofitUtils.getAPIService().doApply(user.getToken(),min,Const.DEVICE_TYPE), new BaseSubscriber<Object>() {
            @Override
            public void onSuccess(Object data) {
                success();
            }
        });
    }

    private void load() {
        final User user= CacheUtils.getInstance(this).getUser();
        if(user==null)return;
        subscribe(RetrofitUtils.getAPIService().userIndex(user.getToken(),Const.DEVICE_TYPE),new BaseSubscriber<User>() {
            @Override
            public void onSuccess(User data) {
                editWalletUrl.setText(data.getWallet_url());
                editWalletUrl.setSelection(data.getWallet_url().length());
            }
        });
    }

    private void success(){
        new CommomDialog2(this, R.style.dialog, "已提交申请，请耐心等待审核",new CommomDialog2.OnCloseListener(){
            @Override
            public void onClick(Dialog dialog, boolean confirm) {
                finish();
            }
        }).setTitle("申请成功").show();
    }

}
